#ifndef CCHERIACHY_H
#define CCHERIACHY_H

#include "qtext/QTree.h"

// #define heriachy data
#include "QtCore"
#include "cocos2d.h"
using namespace cocos2d;

class QCocoNode
{
public :
    CCNode * m_node;
    QString name;
    int id;
    QCocoNode():m_node(0) {}
    QCocoNode(QString name,  int id):m_node(0)
    {
        this->name = name;
        this->id = id;
    }
};

class Coco   
{
public :
    enum NodeType
    {
        T_CCNode = 0x0,
		T_CCLayer  ,
		T_CCMenuItem  ,
		T_CCParticleSystem,
        T_CCScene,
		T_CCSprite,
		T_CCLayerColor,
		T_CCMenu,
        T_CCMenuItemFont,
        T_CCMenuItemToggle,
		T_CCMenuItemSprite,
		T_CCLabelTTF
    };
};




class CocoHieriachy 
{
public:
    static bool m_bInit;
    CocoHieriachy()
    {
        
    }
    ~CocoHieriachy(){delete m_tree;}
    static CocoHieriachy * sharedHieriachy();

    QTree<QCocoNode> *m_tree;
    
    QMap <int , QTreeNode<QCocoNode> * > * m_id_to_node;
    
    void init()
    {
        
        // build CocoNode Hieriachy .
        initRootNode(QCocoNode("CCNode" ,  Coco::T_CCNode) );
        addNode(Coco::T_CCNode , QCocoNode("CCLayer" , Coco::T_CCLayer ));
        addNode(Coco::T_CCNode , QCocoNode("CCMenuItemFont" , Coco::T_CCMenuItemFont ));
		addNode(Coco::T_CCNode , QCocoNode("CCMenuItemSprite" , Coco::T_CCMenuItemSprite ));
        addNode(Coco::T_CCNode , QCocoNode("CCParticleSystem" , Coco::T_CCParticleSystem ));
		addNode(Coco::T_CCNode , QCocoNode("CCLabelTTF" , Coco::T_CCLabelTTF));
//      addNode(Coco::T_CCNode , QCocoNode("CCScene" , Coco::T_CCScene ));
        addNode(Coco::T_CCNode , QCocoNode("CCSprite" ,Coco::T_CCSprite ));
        addNode(Coco::T_CCLayer , QCocoNode("CCLayerColor" , Coco::T_CCLayerColor));
        addNode(Coco::T_CCLayer , QCocoNode("CCMenu" , Coco::T_CCMenu));
        
        
    }
    
    void initRootNode(QCocoNode cocoNode)
    {
        QTreeNode<QCocoNode> *rootNode = new QTreeNode<QCocoNode>(cocoNode); 
        m_tree = new QTree<QCocoNode>(rootNode);
        m_id_to_node = new QMap<int , QTreeNode<QCocoNode> * > ();
        m_id_to_node->insert(cocoNode.id , rootNode);
    }
    
    QTreeNode<QCocoNode> *RootNode()
    {
        return m_tree->RootNode();
    }
    
    
    void addNode(int parentNodeId, QCocoNode cocoNode)
    {
        //parentNode or node can not be null
        QTreeNode<QCocoNode> *treeParentNode = getTreeNodeById(parentNodeId);
        QTreeNode<QCocoNode> *treeNode = new QTreeNode<QCocoNode>(cocoNode);
        treeParentNode->addChild(treeNode);
        m_id_to_node->insert(cocoNode.id , treeNode);
    }
    
    QTreeNode<QCocoNode> * getTreeNodeById(int id )
    {
        return m_id_to_node->value(id);
    }
    
    QCocoNode &getCoCoNodeById(int id)
    {
        return getTreeNodeById(id)->m_data;
    }
    
    QCocoNode * getNodeByName()
    {
        //not implement yet. 
        return NULL;
    }
    
    void printRootTree()
    {
        printTree(m_tree->RootNode(), 0 );
    }
    
    void printTree(QTreeNode <QCocoNode> * node , int level)
    {
		QString s;
		for (int i = 0 ; i< level ; i ++)
		{
			s += "-";
		}
		qDebug() << s +  node->m_data.name  ;
        if (node->hasChildren())
        {
			level ++;
            for (int i = 0 ;i < node->getChildren()->size(); i++)
            {
                printTree(node->getChildren()->at(i) , level);
            }
        }

    }
};















#endif // CCHERIACHY_H
